<template>
    <div>
        <!-- 面包屑导航 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>知识元列表</el-breadcrumb-item>
        </el-breadcrumb>
        <!-- 卡片 -->
        <el-card class="box-card">
            <el-row :gutter="20">
                <el-col :span="4">
                    <el-button type="primary" @click="showAddDialog">添加知识元</el-button>
                </el-col>
            </el-row>
             <!-- 表格 -->
             <el-table :data="KEList"  stripe >
                <el-table-column type="index"></el-table-column>
                <el-table-column label="知识元" prop="content" :filters="contentFilters" :filter-method="filterHandler"></el-table-column>
                <!-- <el-table-column label="关联状态" prop="hasChild" :filters="isConnetedFilters" :filter-method="filterHandler"></el-table-column> --> -->
                <el-table-column label="下级关联状态">
                    <!-- 作用域插槽 -->
                    <template slot-scope="scope">
                        <!-- @change="stateChanged(scope.row)" -->
                        <el-switch v-model="scope.row.hasChild" disabled></el-switch>
                    </template> 
                </el-table-column>

                <el-table-column label="操作">
                     <!-- 作用域插槽  在其中放置一些button-->
                    <template slot-scope="scope">
                        <!-- <el-button type="primary" size="mini" plain @click="showDetail(scope.row.id)">详情</el-button> -->
                        <el-button type="warning" size="mini" plain @click="showEditDialog(scope.row.id)">修改</el-button>
                        <el-button type="danger" size="mini" plain @click="deleteKEById(scope.row.id)">删除</el-button>
                    </template> 
                </el-table-column>

            </el-table>
        </el-card>
        <!-- 新增对话框 -->
        <el-dialog
            title="新增知识元"
            :visible.sync="addDialogVisible"
            width="50%"
            @close="addDialogClosed">
            <!-- 对话框内容主体 -->
            <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="70px">
                <el-form-item label="知识元" prop="content"> <!--prop 是在editFormRule中用的-->
                    <el-input v-model="addForm.content"></el-input>
                </el-form-item>
                <el-form-item label="图片文件素材" prop="imageIdList">
                    <el-select v-model="addForm.imageIdList" multiple placeholder="请选择图片文件素材">
                        <el-option v-for="item in imageOptions" :key="item.id" :value="item.id" :label="item.description"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="pdf文件素材" prop="pdfId">
                    <el-select v-model="addForm.pdfId" placeholder="pdf文件素材">
                        <el-option v-for="item in pdfOptions" :key="item.id" :value="item.id" :label="item.description"></el-option>  
                    </el-select>
                </el-form-item>
            </el-form>
            <!-- 对话框底部 -->
            <span slot="footer" class="dialog-footer">
                <el-button @click="addDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="addKEInfo">确 定</el-button>
            </span>
        </el-dialog>         
        <!-- 修改对话框 -->
        <el-dialog
            title="修改知识元"
            :visible.sync="editDialogVisible"
            width="50%"
            @close="editDialogClosed">
            <!-- 对话框内容主体 -->
            <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="70px">
                <el-form-item label="知识元" prop="content"> <!--prop 是在editFormRule中用的-->
                    <el-input v-model="editForm.content"></el-input>
                </el-form-item>
                <el-form-item label="图片文件素材" prop="imageIdList">
                    <el-select v-model="editForm.imageIdList" multiple placeholder="请选择图片文件素材">
                        <el-option v-for="item in imageOptions" :key="item.id" :value="item.id" :label="item.description"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="pdf文件素材" prop="pdfId">
                    <el-select v-model="editForm.pdfId" placeholder="pdf文件素材">
                        <el-option v-for="item in pdfOptions" :key="item.id" :value="item.id" :label="item.description"></el-option>  
                    </el-select>
                </el-form-item>
            </el-form>
            <!-- 对话框底部 -->
            <span slot="footer" class="dialog-footer">
                <el-button @click="editDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="editKEInfo">确 定</el-button>
            </span>
        </el-dialog> 
    </div>
</template>

<script>
    export default {
        data() {
            return {
                // 查询list参数
                queryInfo: {
                    pageNum:1,
                    pageSize:20,
                    query:''
                },
                // 图片下拉列表选项
                imageOptions:[
                    // {id:'58', description: '特里尔今貌'},
                    // {id:"76", description:"19世纪30年代的特里尔"}
                ], 
                // pdf下拉列表选项
                pdfOptions: [
                    // {id: '538',description: '特里尔介绍'}, 
                    // {id: '555',description: '马克思家谱介绍'}
                ],
                // 对话框可视化
                editDialogVisible: false,
                addDialogVisible: false,
                // 事件列表总列表
                KEList: [
                    // {id: "172", content:"马克思诞生于德国特里尔市", hasChild:0},
                    // {id: "173", content:"恩格斯诞生于德国巴门市", hasChild:1}
                ],
                // 编辑对话框绑定数据
                editForm: {
                    content:"",
                    imageIdList:[],  // 图片id 列表
                    pdfId:"" // pdf id
                },
                // 新增对话框绑定数据
                addForm: {
                    content:"",
                    imageIdList:[],  // 图片id 列表
                    pdfId:"" // pdf id
                },
                // 添加表单的验证规则对象
                editFormRules:{
                    content:[
                        { required: true, message: '请输入知识元', trigger: 'blur' },
                        { min: 2, max: 100, message: '长度在2~100个字符', trigger: 'blur' }
                    ]
                },
                // 添加表单的验证规则对象
                addFormRules:{
                    content:[
                        { required: true, message: '请输入知识元', trigger: 'blur' },
                        { min: 2, max: 100, message: '长度在2~100个字符', trigger: 'blur' }
                    ]
                },
                // 事件列表过滤内容
                contentFilters:[]
            }
        },
        created() {
            this.getKEList();
        },
        methods: {
            // 展示编辑事件的对话框（修改）
            async showAddDialog() {
                this.pdfOptions = [{id:null, description:"不选"}];
                this.imageOptions = [];
                this.addDialogVisible = true;
                const{data:resImage} = await this.$http.get("fileNode/selectAll", {params:{scene:"image"}});
                const imageFileNodeList = resImage.data;
                for (let i = 0; i < imageFileNodeList.length; i++) {
                    const imageFileNode = imageFileNodeList[i];
                    this.imageOptions.push({id:imageFileNode.id, description:imageFileNode.description})
                }
                
                const{data:resPdf} = await this.$http.get("fileNode/selectAll", {params:{scene:"pdf"}});
                const pdfFileNodeList =  resPdf.data
                for (let i = 0; i < pdfFileNodeList.length; i++) {
                    const pdfFileNode = pdfFileNodeList[i];
                    this.pdfOptions.push({id:pdfFileNode.id, description:pdfFileNode.description})
                }
                
            },
            // 展示编辑事件的对话框（修改）
            async showEditDialog(id) {
                this.imageOptions = [];
                this.pdfOptions = [{id:null, description:"不选"}];
                this.editDialogVisible = true;
                const{data:res} = await this.$http.get("knowledgeElement/selectOne", {params:{"id":id}});
                this.editForm = res.data;
                const{data:resImage} = await this.$http.get("fileNode/selectAll", {params:{scene:"image"}});
                const imageFileNodeList = resImage.data;
                for (let i = 0; i < imageFileNodeList.length; i++) {
                    const imageFileNode = imageFileNodeList[i];
                    this.imageOptions.push({id:imageFileNode.id, description:imageFileNode.description})
                }

                const{data:resPdf} = await this.$http.get("fileNode/selectAll", {params:{scene:"pdf"}});
                const pdfFileNodeList = resPdf.data;
                for (let i = 0; i < pdfFileNodeList.length; i++) {
                    const pdfFileNode = pdfFileNodeList[i];
                    this.pdfOptions.push({id:pdfFileNode.id, description:pdfFileNode.description})
                }

            },
            // 修改对话框点击了确定事件
            editKEInfo() {
                this.$refs.editFormRef.validate(async valid =>{
                    if (!valid) return
                    // 发起请求
                    const {data:res} = await this.$http.post("knowledgeElement/update", this.editForm);
                    if (res.code !== 200) {
                        return this.$message.error("更新失败");
                    }
                    // 关闭对话框
                    this.editDialogVisible = false;
                    // 刷新列表
                    this.getKEList();
                    // 提示更新成功
                    this.$message.success("更新成功");
                } )
            },
            // 添加对话框点击了确定事件
            addKEInfo() {
                this.$refs.addFormRef.validate(async valid =>{
                    if (!valid) return
                    // 发起请求
                    console.log(this.addForm);
                    const {data:res} = await this.$http.post("knowledgeElement/insert", this.addForm);
                    if (res.code !== 200) {
                        return this.$message.error("更新失败");
                    }
                    // 关闭对话框
                    this.addDialogVisible = false;
                    // 刷新列表
                    this.getKEList();
                    // 提示更新成功
                    this.$message.success("更新成功");
                } )
            },
            // 刷新事件列表
            async getKEList() {
                const {data:res} = await this.$http.get("knowledgeElement/selectAll", {params: this.queryInfo})
                if (res.code != 200) return this.$message.error("获取知识元列表失败!");
                this.KEList = res.data
                for (let i = 0; i < this.KEList.length; i++) {
                    const cur_KE = this.KEList[i];

                    var contentObj = new Object();
                    contentObj.text = cur_KE.content;
                    contentObj.value = cur_KE.content;
                    this.contentFilters.push(contentObj);
                }
                // this.KEList = res.data.KEList;
                // this.total = res.data.total;
            },
            // 表格过滤函数
            filterHandler(value, row, column) {
                const property = column['property'];
                return row[property] === value;
            },
            // 对话框关闭清空域
            editDialogClosed() {
                this.$refs.editFormRef.resetFields();
                this.editForm.imageIdList = [];
                this.editForm.pdfId = "";
            },
            addDialogClosed() {
                this.$refs.addFormRef.resetFields();
                this.addForm.imageIdList = [];
                this.addForm.pdfId = "";                
            },
            // 删除操作
            async deleteKEById(id) {
                // 弹框提示
                const conformRes = await this.$confirm('此操作将删除该条, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'  // 图标
                }).catch(err => {
                    return err
                }) 
                // 如果用户确认则返回confirm串 取消返回的是cancel
                // console.log(conformRes)
                if (conformRes !== "confirm") {
                    return this.$message.info("已经取消删除");
                }
                const{data:res} = await this.$http.post("knowledgeElement/deleteById", {id:id});
                if (res.code !== 200) {
                    return this.$message.error(res.meg);
                }
                this.$message.success("删除成功");
                this.getKEList();
            }
        }
    }
</script>

<style lang="less" scoped>
    .box-card {
        line-height: 0%;
        text-align: left;
    }
</style>